package com.project.weimi.activity;

import java.util.HashMap;
import java.util.Map;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.entity.response.ResponseBean;
import com.project.weimi.interfaces.MyCallBack;
import com.project.weimi.utils.GsonUtil;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.utils.XUtil;
import com.project.weimi.widget.ToastMessage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * 修改婚姻状况Actitivy
 * @author Administrator
 *
 */
public class MarriageActivity extends BaseActivity{

	private RelativeLayout rl_single,rl_love,rl_married,rl_divorce;
	private TextView tv_single,tv_love,tv_married,tv_divorce;
	private ImageView img_check_single,img_check_love,img_check_married,img_check_divorce;
	private ToggleButton btn_toggle;
	
	private RelativeLayout rls[] = new RelativeLayout[4];
	private ImageView[] ivs = new ImageView[4];
	private TextView[] tvs = new TextView[4];
	
	private String userPhone;
	private String marriageState;
	String mSign;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_marriage, "婚恋状况", true, "保存");
		
		// 获得从个人信息页面传来的婚姻状况
		mSign = getIntent().getStringExtra("sign");
		
		initView();
		
	}
	
	private void initView(){
		rl_single = (RelativeLayout)findViewById(R.id.rl_single);
		rl_love = (RelativeLayout)findViewById(R.id.rl_love);
		rl_married = (RelativeLayout)findViewById(R.id.rl_married);
		rl_divorce = (RelativeLayout)findViewById(R.id.rl_divorce);
		
		rls[0] = rl_single;
		rls[1] = rl_love;
		rls[2] = rl_married;
		rls[3] = rl_divorce;
		
		tv_single = (TextView)findViewById(R.id.tv_single);
		tv_love = (TextView)findViewById(R.id.tv_love);
		tv_married = (TextView)findViewById(R.id.tv_married);
		tv_divorce = (TextView)findViewById(R.id.tv_divorce);
		
		tvs[0] = tv_single;
		tvs[1] = tv_love;
		tvs[2] = tv_married;
		tvs[3] = tv_divorce;
		
		img_check_single = (ImageView)findViewById(R.id.img_check_single);
		img_check_love = (ImageView)findViewById(R.id.img_check_love);
		img_check_married = (ImageView)findViewById(R.id.img_check_married);
		img_check_divorce = (ImageView)findViewById(R.id.img_check_divorce);
		
		ivs[0] = img_check_single;
		ivs[1] = img_check_love;
		ivs[2] = img_check_married;
		ivs[3] = img_check_divorce;
		
		btn_toggle = (ToggleButton)findViewById(R.id.btn_toggle);
		
		showMarriageState(tvs,ivs);
		
		// 监听
		btn_toggle.setOnCheckedChangeListener(checkChangeListener);
		SetClickListener.setClickListener(listener, rl_single,rl_love,rl_married,rl_divorce,bar.getRightBar());
		
	}
	
	// 显示婚姻状态
	private void showMarriageState(TextView tvs[],ImageView ivs[]){
		for (int i = 0; i < ivs.length; i++) {
			if(TextUtils.isEmpty(mSign)){
				ivs[i].setVisibility(View.GONE);
			} else {
				if(tvs[i].getText().toString().equals(mSign)){
					ivs[i].setVisibility(View.VISIBLE);
				} else {
					ivs[i].setVisibility(View.GONE);
				}
			}
		}
	}
	
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.rl_single:
				selectMarriageState(0,ivs);
				break;
			case R.id.rl_love:
				selectMarriageState(1,ivs);
				break;
			case R.id.rl_married:
				selectMarriageState(2,ivs);
				break;
			case R.id.rl_divorce:
				selectMarriageState(3,ivs);
				break;
			case R.id.right_bar:
				
				if(!NetUtils.isConnected(MarriageActivity.this)){
					ToastMessage.showToast(MarriageActivity.this, "网络不给力，请检查网络设置");
				} else {
					initHttp();
				}
				
				break;
			}
		}
	};
	
	private OnCheckedChangeListener checkChangeListener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(isChecked){
				ToastMessage.showMyToast(MarriageActivity.this, "已开启");
			}else{
				ToastMessage.showMyToast(MarriageActivity.this, "已关闭");
			}
		}
	};
	
	private void initHttp(){
		getTextValue(tvs);
		
		if(img_check_single.getVisibility() == View.GONE && img_check_love.getVisibility() == View.GONE
				&& img_check_married.getVisibility() == View.GONE && img_check_divorce.getVisibility() == View.GONE){
			ToastMessage.showMyToast(MarriageActivity.this, "请选择您的婚姻状况");
			return;
		}
		if(TextUtils.isEmpty(userPhone)){
			return;
		}
		if(marriageState.equals(mSign)){
			ToastMessage.showMyToast(MarriageActivity.this, "昵称与原来保持相同，未做修改");
			finish();
			return;
		}
		
		//Log.e("TAG", "MarriageActivity====状况---"+marriageState);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", userPhone);
		map.put("Marital", marriageState);
		XUtil.post(Constant.CHANGE_MARRIAGE, map, new MyCallBack<String>(){
			
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(null != result && !"".equals(result)){
					//Log.e("TAG", "MarriageActivity---result==="+result);
					ResponseBean bean = (ResponseBean) GsonUtil.jsonToBean(
							result, ResponseBean.class);
					if(null != bean){
						String str = bean.getResult();
						if(!TextUtils.isEmpty(str) && "1".equals(str)){
							ToastMessage.showMyToast(MarriageActivity.this, "修改成功");
							Intent intent = new Intent();
							intent.putExtra("marriageState", marriageState);
							setResult(3, intent);
							finish();
						}
					}
				}
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if(null != spHelper && !TextUtils.isEmpty(memberId)){
			//Log.e("TAG", "onStart----MarriageActivity前memberId===---"+memberId);
			userPhone = memberId;
			//Log.e("TAG", "onStart----MarriageActivity后userPhone===---"+userPhone);
		}
	}
	
	// 点击时选择状态
	private void selectMarriageState(int index,ImageView ivs[]){
		for (int i = 0; i < rls.length; i++) {
			if(i == index){
				ivs[i].setVisibility(View.VISIBLE);
			}
			else {
				ivs[i].setVisibility(View.GONE);
			}
		}
	}
	
	// 保存选中的值
	private void getTextValue(TextView tvs[]){
		for (int i = 0; i < ivs.length; i++) {
			if(ivs[i].getVisibility() == View.VISIBLE){
				ToastMessage.showMyToast(MarriageActivity.this, "保存"+tvs[i].getText().toString());
				marriageState = tvs[i].getText().toString();
				//Log.e("TAG", "MarriageActivity====状况---循环后=="+marriageState);
			}
		}
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
	
	
}
